<template>
  <el-dialog
    :before-close="handleClose"
    title="修改密码"
    :visible.sync="dialogVisible"
  >
    <el-form
      ref="ruleForm"
      v-loading="loading"
      label-width="100px"
      :model="ruleForm"
      :rules="rules"
      size="mini"
    >
      <el-form-item label="原密码" prop="ori_password">
        <el-input
          v-model="ruleForm.ori_password"
          placeholder="请输入原密码"
          type="password"
        />
      </el-form-item>
      <el-form-item label="新密码" prop="new_password">
        <el-tooltip>
          <div slot="content">
            1. 密码长度需大于12位<br />
            2. 密码需同时包含大小写英文字符、数字与特殊符号<br />
            3. 密码中不允许出现任何全角字符
          </div>
          <el-input
            v-model="ruleForm.new_password"
            placeholder="请输入新密码"
            type="password"
          />
        </el-tooltip>
      </el-form-item>
      <el-form-item label="确认密码" prop="password_again">
        <el-input
          v-model="ruleForm.password_again"
          placeholder="请再次输入密码"
          type="password"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('ruleForm')">保存</el-button>
        <el-button @click="dialogVisible = false">返回</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import clearWatch from "@/utils/mixin/clearWatch";
import user from "@/api/user";

export default {
  name: "ResetPassword",
  mixins: [clearWatch],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      ruleForm: {
        ori_password: "",
        new_password: "",
        password_again: ""
      },
      rules: {
        ori_password: [
          { required: true, message: "请输入原密码", trigger: "blur" },
          { min: 6, max: 18, message: "长度在 6 到 18 个字符", trigger: "blur" }
        ],
        new_password: [
          { required: true, message: "请输入新密码", trigger: "blur" }
        ],
        password_again: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("请再次输入密码"));
              } else if (value !== this.ruleForm.new_password) {
                callback(new Error("两次输入密码不一致!"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    handleClose(done) {
      done();
    },
    show() {
      this.dialogVisible = !this.dialogVisible;
    },

    async submit(formName) {
      let userInfo = await user
        .info()
        .then(res => {
          return res.data;
        })
        .catch(() => {
          return false;
        });
      this.$refs[formName].validate(valid => {
        if (valid) {
          let data = Object.assign({}, this.ruleForm);
          delete data.password_again;
          data.user_id = userInfo.id;
          user.chpasswd(data).then(res => {
            this.$message.success(res.data.message);
            this.dialogVisible = false;
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
